package com.dytj.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dytj.pojo.Administration;
import com.dytj.service.AdministrationServiceImpl;

@WebServlet("/AdministrationServlet")
public class AdministrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	public AdministrationServiceImpl administrationDao = new AdministrationServiceImpl();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		Administration administration = new Administration();
		String accounts = request.getParameter("accounts");
		administration.setAccounts(accounts);
		String password = request.getParameter("password");
		administration.setPassword(password);
		
//		System.out.println("1accounts:"+accounts+" password:"+password);
//		System.out.println("2accounts:"+accounts+" password:"+password);
		List<Administration> list = new ArrayList<Administration>();
		try {
			list = administrationDao.adminCheck(administration);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(list.toString());
		
		if(!list.isEmpty()) {
//			System.out.println("ok");
			Administration value = new Administration();
			value.setAccounts(accounts);
			value.setPassword(password);
			
			request.getSession(true).setAttribute("administration", value);
			String info = "ok！";
			request.setAttribute("outputMessage", info);
			request.getRequestDispatcher("/WEB-INF/jsp/info.jsp").forward(request,response);
		}else {
			String info = "用户名或密码不正确！";
			request.setAttribute("outputMessage", info);
			request.getRequestDispatcher("/WEB-INF/jsp/info.jsp").forward(request,response);
		}
		
	}
}
